<template>
  <div class="system-user-dialog-container">
    <el-dialog :close-on-click-modal="false" :title="dataForm.userId ? $t('common.editBtn') : $t('common.addBtn')"
      draggable v-model="visible">
      <el-form :model="dataForm" :rules="dataRules" label-width="100px" ref="dataFormRef" v-loading="loading">
        <el-row>
          <el-col :span="12" class="mb20">
            <el-form-item :label="$t('message.userNo')" prop="WorkNumber">
              <el-input v-model="dataForm.WorkNumber"></el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item :label="$t('message.userName')" prop="Name">
              <el-input v-model="dataForm.Name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" class="mb20">
            <el-form-item :label="$t('productlineUser.deptartment')" prop="Department">
              <el-input v-model="dataForm.Department"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item :label="$t('productlineUser.tel')" prop="tel">
              <el-input v-model="dataForm.tel"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" class="mb20">
            <el-form-item :label="$t('productlineUser.isOut')" prop="isOut">
              <el-select v-model="dataForm.isOut" placeholder="Select" style="width: 160px">
                <el-option v-for="item in isoutOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="close">{{ $t('common.cancelButtonText') }}</el-button>
          <el-button @click="onSubmit" type="primary" icon="check" :disabled="loading">{{
            $t('common.confirmButtonText')
          }}</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="spDialog" setup>
import { useMessage } from '/@/hooks/message';
import { ref, reactive } from 'vue';
import { AddOrUpdateUser } from '/@/api/platform/mes/productlineUser';
// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
// 通知父组件刷新
const emit = defineEmits(['refresh']);

const dataForm = reactive({
});
const dataRules = ref({
  isOut: [
    { required: true, message: t('bar.cannotEmpty'), trigger: 'blur' },
  ],
  WorkNumber: [
    { required: true, message: t('bar.cannotEmpty'), trigger: 'blur' },
  ],
  Name: [
    { required: true, message: t('bar.cannotEmpty'), trigger: 'blur' },
  ]
});
// 打开弹窗
const openDialog = async () => {
  visible.value = true;
  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

};
const isoutOptions = ref([
  { label: '', value: '' },
  { label: '否', value: 0 },
  { label: '是', value: 1 },
]);
// 初始化数据
onMounted(() => {
});
// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    await AddOrUpdateUser(dataForm).then((res) => {
      if (!res.ok) {
        useMessage().error(res.data);
      }
    });
    visible.value = false; // 关闭弹窗
    emit('refresh');
  } catch (error: any) {
    useMessage().error(error.msg);
  } finally {
    loading.value = false;
  }
};
// 暴露变量
defineExpose({
  openDialog,
});
</script>
